package com.haoxi.gateway.cmpp30msg;

/**
 * Title:CMPP3.0,2004.09
 */

import java.io.IOException;
import java.io.InputStream;

/**
 * ʵ��CMPP3.0 CMPPSubmitRep��
 */
public class CMPPSubmitRep {
    private byte bodybytes[];
    private String Msg_Id;
    private int result = -1;

    /**
     * ����һ��CMPPSubmitRep
     */
    public CMPPSubmitRep() {
        bodybytes = new byte[CMPPCommon.MSG_SUBMIT_REP_BODY_LENGTH];
    }

    /**
     * ���ֽ����鷽ʽ����һ��CMPPSubmitRep��
     * 
     * @param body
     * @throws CMPP30Exception
     */
    public CMPPSubmitRep(byte[] body) throws CMPP30Exception {
        if (body.length != CMPPCommon.MSG_SUBMIT_REP_BODY_LENGTH) {
            throw new CMPP30Exception("invalid CMPPSubmitRep body package!");
        }
        bodybytes = body;
        parseBody();
    }

    /**
     * ���ֽڷ�ʽ��ȡ���
     * 
     * @param in ������
     * @throws IOException ��IOΥ��ʱ
     */
    public void read(InputStream in) throws IOException {
        CMPPMsgHead head = new CMPPMsgHead();
        head.read(in);
        if (head.getCommandID() == 0x80000004) {
            in.read(bodybytes);
        }
        parseBody();
    }

    /**
     * �ֽ��ֽ������
     */
    private void parseBody() {
        byte[] temp = new byte[8];
        System.arraycopy(bodybytes, 0, temp, 0, 8);
        // CMPPCommon.BytesCopy(bodybytes, temp, 0, temp.length - 1, 0);
        // MainSystem.logPrint("Msg_id=");
        Msg_Id = "";
        for (int i = 0; i < temp.length; i++) {
            Msg_Id += temp[i];
        }
        // MainSystem.logPrint(Msg_Id);
        // result = Integer.parseInt(new String(bodybytes,8,11).trim());
        byte[] aresult = new byte[4];
        System.arraycopy(bodybytes, 8, aresult, 0, 4);
        // CMPPCommon.BytesCopy(bodybytes, aresult, 8, 11, 0);
        /*
         * String tmpResult=""; for (int i=0;i<4;i++) { tmpResult += temp[i];
         * //MainSystem.logPrint(" "+temp[i]); }
         */

        try {
            result = CMPPCommon.Bytes4ToInt(aresult);
        } catch (Exception e) {
            result = -2;
        }

        // result = bodybytes[8];
        // main.MainSystem.logPrint ( "CMPPSubmitRep result=" + result );
    }

    /**
     * ȡ�÷��ص�Msg_Id
     * 
     * @return ���ص�Msg_Id
     */
    public String getMsg_Id() {
        return Msg_Id;
    }

    public byte[] getMsgIdbytes() {
        byte[] result = new byte[8];
        System.arraycopy(bodybytes, 0, result, 0, 8);
        return result;
    }

    /**
     * ȡ�÷��ص�Result
     * 
     * @return ���ص�Result
     */
    public int getResult() {
        return result;
    }
}